package com.znh.entity;

import com.znh.enums.PieceActionEnum;
import com.znh.enums.PieceInitLocationEnum;
import com.znh.enums.PiecePlayerEnum;

import java.awt.*;
import java.util.HashSet;

/**
 * @version 1.0
 * @Author znh
 * @Date 2024/5/24 下午3:21
 * @注释 将帅类
 */
public class Boss extends Piece {

    public Boss(int id, Point point, PiecePlayerEnum player, PieceInitLocationEnum initLocation) {
        super(id, "boss", point, player, initLocation, new StringBuilder().append("images/boss")
                .append(player == PiecePlayerEnum.RED ? "0" : "1").append(".png").toString());
    }

    @Override
    public boolean isMove(Point tp) {
        PieceActionEnum action = action(tp);
        //是否在九宫内、是否直线移动、是否移动一步
        return isJiuGong(tp) &&
                ((action == PieceActionEnum.LINEX && getStep(tp, PieceActionEnum.LINEX) == 1)
                        || (action == PieceActionEnum.LINEY && getStep(tp, PieceActionEnum.LINEY) == 1));
    }

    @Override
    public boolean simulateMove(Point tp) {
        return isMove(tp) && super.simulateMove(tp);
    }

    /**
     * 判断是否被将军
     * 对方的将：是否Y轴直线、中间是否没有棋子阻挡
     * 对方的车：是否直线、中间是否没有棋子阻挡
     * 对方的马：是否走日字、是否蹩脚
     * 对方的炮：是否直线、中间是否有且仅有一个棋子
     * 对方的兵：是否直线、是否后退、是否一步
     *
     * @return true:被将军 false:不被将军
     */
    public boolean isNextEat() {
        return GameRule.NAME_PIECE_MAP.get(this.getPlayer() == PiecePlayerEnum.RED ? PiecePlayerEnum.BLACK : PiecePlayerEnum.RED)
                .entrySet().stream().anyMatch(stringHashSetEntry -> {
                    String name = stringHashSetEntry.getKey();
                    HashSet<Piece> pieces = stringHashSetEntry.getValue();
                    switch (name) {
                        case "boss":
                            //是否Y轴直线、中间是否没有棋子阻挡
                            return pieces.stream().anyMatch(piece -> piece.action(this.getPoint()) == PieceActionEnum.LINEY
                                    && piece.countRoutePiece(this.getPoint(), PieceActionEnum.LINEY) == 0);
                        case "bing":
                            //是否直线、是否后退、是否一步
                        case "che":
                            //是否直线、中间是否没有棋子阻挡
                        case "ma":
                            //对方的马：是否走日字、是否蹩脚
                        case "pao":
                            //是否直线、中间是否有且仅有一个棋子
                            return pieces.stream().anyMatch(piece -> piece.isMove(this.getPoint()));
                        default:
                            return false;
                    }
                });
    }
}
